package com.zw.mes.module.cost.service;




import com.zw.mes.entity.cost.CosProcess;
import com.zw.mes.entity.cost.CosProcessDetail;
import com.zw.mes.entity.sys.Dept;
import com.zw.mes.entity.sys.User;
import com.zw.mes.module.cost.mapper.*;
import com.zw.mes.module.cost.model.*;

import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: China.wsg
 * @Date: 2020/11/10 15:11
 */
@Service
@SuppressWarnings("all")
public class CostService {


    @Autowired
    private LogService logService;
    @Autowired
    private CosSheetRepository cosSheetRepository;
    @Autowired
    private CosDetailRepository cosDetailRepository;
//    @Autowired
//    private DsnProStructRepository dsnProStructRepository;
//    @Autowired
//    private VerifyMapper verifyMapper;
    @Autowired
    private CosDetailRepository costMapper;
    @Autowired
    private SheetMapper dsnCostMapper;
    @Autowired
    private CosCarriageDetailMapperNew cosCarriageDetailMapper;

    @Autowired
    private CosMachiningDetailRepository cosMachiningDetailRepository;
    /**
     * 项目编码是否存在
     *
     * @param param
     * @return
     */
    public Boolean codeExist(String proCode) {
        Boolean boolExist;
        List<Project> projects = dsnCostMapper.findProjectByCode(proCode);
        if (projects == null || projects.size() == 0) {
            boolExist = false;
        } else {
            boolExist = true;
        }
        return boolExist;
    }

    /**
     * 项目信息
     *
     * @param proCode
     * @return
     */
    public List<Project> findProjectByCode(String proCode) {
        return dsnCostMapper.findProjectByCode(proCode);
    }

    /**
     * 保存设计工时确认单
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveDsnCost(CosSheet cosSheet, Double[] hQuantity, String[] hRemark, String[] wDescribe, Double[] wQuantity, String[] wUnit, String[] wRemark) {

        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -1;
        }
        // 保存主表
        cosSheet = saveCostSheet(cosSheet);
        // 保存明细
        saveDsnCostDetail(cosSheet, hQuantity, hRemark, wDescribe, wQuantity, wUnit, wRemark);
        logService.saveLog("保存设计工时确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 修改设计工时确认单
     */
    @Transactional(rollbackFor = Exception.class)
    public int editDsnCost(CosSheet cosSheet, Double[] hQuantity, String[] hRemark, String[] wDescribe, Double[] wQuantity, String[] wUnit, String[] wRemark) {

        // 重复校验
        if (!boolCheckEdit(cosSheet)) {
            return -1;
        }
        // 保存主表
        cosSheet = saveCostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveDsnCostDetail(cosSheet, hQuantity, hRemark, wDescribe, wQuantity, wUnit, wRemark);
        logService.saveLog("修改设计工时确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 保存设计工时确认单(明细)
     */
    private void saveDsnCostDetail(CosSheet cosSheet, Double[] hQuantity, String[] hRemark, String[] wDescribe, Double[] wQuantity, String[] wUnit, String[] wRemark) {

        // 人工工时
        CosDetail cosDetailOne = new CosDetail();
        cosDetailOne.setCosSheetId(cosSheet.getId());
        cosDetailOne.setDataType("人工工时");
        cosDetailOne.setDescribe("设计工时");
        cosDetailOne.setQuantity(hQuantity[0]);
        cosDetailOne.setUnit("工日");
        if (hRemark.length > 0) {
            cosDetailOne.setRemark(hRemark[0]);
        }
         cosDetailRepository.save(cosDetailOne);

        // 设备重量
        for (int i = 0; i < wDescribe.length; i++) {
            CosDetail cosDetailTwo = new CosDetail();
            cosDetailTwo.setCosSheetId(cosSheet.getId());
            if (wDescribe[i].equals("结构总重")) {
                cosDetailTwo.setDataType("结构总重");
            } else {
                cosDetailTwo.setDataType("结构净重");
            }
            cosDetailTwo.setDescribe(wDescribe[i]);
            cosDetailTwo.setQuantity(wQuantity[i]);
            cosDetailTwo.setUnit(wUnit[i]);
            if (wRemark.length > 0) {
                cosDetailTwo.setRemark(wRemark[i]);
            }
            cosDetailRepository.save(cosDetailTwo);
        }
    }

    /**
     * 保存调试工时确认单(明细)
     */
    private void saveDsnCostDetail(CosSheet cosSheet, Double[] hQuantity, String[] hRemark) {

        // 调试工时
        CosDetail cosDetailOne = new CosDetail();
        cosDetailOne.setCosSheetId(cosSheet.getId());
        cosDetailOne.setDataType("调试工时");
        cosDetailOne.setDescribe("调试工时");
        cosDetailOne.setQuantity(hQuantity[0]);
        cosDetailOne.setUnit("工日");
        if (hRemark.length > 0) {
            cosDetailOne.setRemark(hRemark[0]);
        }
         cosDetailRepository.save(cosDetailOne);
    }

    /**
     * 保存调试工时确认单
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveDebugCost(CosSheet cosSheet, Double[] hQuantity, String[] hRemark) {

        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -1;
        }
        // 保存主表
        cosSheet = saveCostSheet(cosSheet);
        // 保存明细
        saveDsnCostDetail(cosSheet, hQuantity, hRemark);
        logService.saveLog("保存调试工时确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 修改调试工时确认单
     */
    @Transactional(rollbackFor = Exception.class)
    public int editDebugCost(CosSheet cosSheet, Double[] hQuantity, String[] hRemark) {

        // 重复校验
        if (!boolCheckEdit(cosSheet)) {
            return -1;
        }
        // 保存主表
        cosSheet = saveCostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveDsnCostDetail(cosSheet, hQuantity, hRemark);
        logService.saveLog("修改调试工时确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 删除成本明细
     */
    private void delDsnCostDetail(CosSheet cosSheet) {
        costMapper.delDsnCostDetailByCosSheetId(cosSheet);
    }

    /**
     * 删除机加明细
     */
    private void delMachiningDetail(CosSheet cosSheet) {
        costMapper.delMachiningDetailByCosSheetId(cosSheet);
    }

    /**
     * 保存主表信息（共通调用，无需修改）
     *
     * @param cosSheet
     * @return
     */
    private CosSheet saveCostSheet(CosSheet cosSheet) {

        if (cosSheet.getSpecial().equals("机械")) {
            cosSheet.setBusSpecialId(1);
        } else if (cosSheet.getSpecial().equals("自动化")) {
            cosSheet.setBusSpecialId(2);
        } else if (cosSheet.getSpecial().equals("流体")) {
            cosSheet.setBusSpecialId(3);
        }
        if(cosSheet.getProPartId()!= null && cosSheet.getProPartId() != 0)
        {
            DsnProStruct dsnProStruct = dsnCostMapper.getOne(cosSheet.getProPartId());
            if (dsnProStruct != null)
            {
                cosSheet.setProPart(dsnProStruct.getProPart());
            }
        }

        User user = ShiroUtils.getUser();
        cosSheet.setCreateCode(user.getAccount());
        cosSheet.setCreateName(user.getName());

        // 取得待审核人ID（所长）
//        cosSheet.setVerifyId(dsnCostMapper.findVerifier(user.getId(), 5));
        cosSheet.setBranchId(user.getDeptId());

        Dept M=dsnCostMapper.selectDeptNameById(user.getId());
        cosSheet.setBranchName(M.getName());
//        cosSheet.setBranchName(user.getBranchName());
        cosSheetRepository.save(cosSheet);
        return cosSheet;
    }

    private CosSheet saveUpdateCostSheet(CosSheet cosSheet) {

        if (cosSheet.getSpecial().equals("机械")) {
            cosSheet.setBusSpecialId(1);
        } else if (cosSheet.getSpecial().equals("自动化")) {
            cosSheet.setBusSpecialId(2);
        } else if (cosSheet.getSpecial().equals("流体")) {
            cosSheet.setBusSpecialId(3);
        }
        DsnProStruct dsnProStruct = dsnCostMapper.getOne(cosSheet.getProPartId());
        cosSheet.setProPart(dsnProStruct.getProPart());
        User user = ShiroUtils.getUser();
        cosSheet.setCreateCode(user.getAccount());
        cosSheet.setCreateName(user.getName());

        // 取得待审核人ID（所长）
//        cosSheet.setVerifyId(dsnCostMapper.findVerifier(user.getId(), 5));
        cosSheet.setBranchId(user.getDeptId());

        Dept M=dsnCostMapper.selectDeptNameById(user.getId());
        cosSheet.setBranchName(M.getName());
//        cosSheet.setBranchName(user.getBranchName());
        cosSheetRepository.save(cosSheet);
        return cosSheet;
    }


    /**
     * 保存主表信息（共通调用，无需修改）
     *
     * @param cosSheet
     * @return
     */
    private CosSheet updCostSheet(CosSheet cosSheet) {

        if (cosSheet.getSpecial().equals("机械")) {
            cosSheet.setBusSpecialId(1);
        } else if (cosSheet.getSpecial().equals("自动化")) {
            cosSheet.setBusSpecialId(2);
        } else if (cosSheet.getSpecial().equals("流体")) {
            cosSheet.setBusSpecialId(3);
        }
        DsnProStruct dsnProStruct = dsnCostMapper.getOne(cosSheet.getProPartId());
        cosSheet.setProPart(dsnProStruct.getProPart());
        User user = ShiroUtils.getUser();
        cosSheet.setCreateCode(user.getAccount());
        cosSheet.setCreateName(user.getName());

        // 取得待审核人ID（所长）
//        cosSheet.setVerifyId(dsnCostMapper.findVerifier(user.getId(), 19));
        cosSheet.setBranchId(user.getDeptId());
        /**
         * 后期在解决这块
         */
        cosSheet.setBranchName("");
//        cosSheet.setBranchName(user.getBranchName());
        cosSheetRepository.update(cosSheet);
        return cosSheet;
    }



    private CosSheet upd2CostSheet(CosSheet cosSheet) {

        if (cosSheet.getSpecial().equals("机械")) {
            cosSheet.setBusSpecialId(1);
        } else if (cosSheet.getSpecial().equals("自动化")) {
            cosSheet.setBusSpecialId(2);
        } else if (cosSheet.getSpecial().equals("流体")) {
            cosSheet.setBusSpecialId(3);
        }
        DsnProStruct dsnProStruct = dsnCostMapper.getOne(cosSheet.getProPartId());
        cosSheet.setProPart(dsnProStruct.getProPart());
        User user = ShiroUtils.getUser();
        cosSheet.setCreateCode(user.getAccount());
        cosSheet.setCreateName(user.getName());

        // 取得待审核人ID（所长）
//        cosSheet.setVerifyId(dsnCostMapper.findVerifier(user.getId(), 19));
        cosSheet.setBranchId(user.getDeptId());
        /**
         * 后期在解决这块
         */
        cosSheet.setBranchName("");
//        cosSheet.setBranchName(user.getBranchName());
        cosSheetRepository.update2(cosSheet);
        return cosSheet;
    }
    /**
     * 校验是否允许保存
     *
     * @param cosSheet
     * @return
     */
    private boolean boolCheckSave(CosSheet cosSheet) {

        Integer count = cosSheetRepository.findCosSheetCountByQuery(cosSheet);
        if (count.compareTo(0) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 校验是否允许修改
     *
     * @param cosSheet
     * @return
     */
    private boolean boolCheckEdit(CosSheet cosSheet) {

        Integer count = cosSheetRepository.findCosSheetCountRemoveMyselfByQuery(cosSheet);
        if (count.compareTo(0) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 校验是否允许撤销
     *
     * @param ids
     * @return
     */
    private boolean boolCheckCancel(String ids) {

        Integer count = cosSheetRepository.findCosSheetCountReceiveById(ids);
        if (count.compareTo(0) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 根据条件取得成本主表
     *
     * @param cosSheet
     * @return
     */
    public List<CosSheet> findCosSheetByQuery(CosSheet cosSheet) {
        return cosSheetRepository.findCosSheetByQuery(cosSheet);
    }

    /**
     * 按ID查询成本信息
     *
     * @param id
     * @return
     */
    public CosSheet getCosSheetById(Integer id) {
        return cosSheetRepository.getOne(id);
    }

    /**
     * 按 主单ID 查询成本明细（共通调用，无需修改）
     *
     * @param cosSheetId
     * @return
     */
    public List<CosDetail> findCosDetailByCosSheetIdAndDataType(Integer cosSheetId, String dataType) {
        return cosDetailRepository.findCosDetailByCosSheetIdAndDataType(cosSheetId, dataType);
    }

    /**
     * 查找运输
     * @return
     */
    public CosCarriageDetail findCosTraDetailByCosSheetId(Integer cosSheetId){
        return cosCarriageDetailMapper.findOne(cosSheetId);
    }
    /**
     * 删除成本（共通调用，逻辑删除）
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer delCostById(Integer id, String costType) {

        CosSheet cosSheet = cosSheetRepository.getOne(id);

        logService.saveLog("删除" + costType + "成本信息", "主单ID：" + id+ "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        //cosSheet.setIsDel(1);
        cosSheetRepository.del(id);
        return 1;
    }

    /**
     * 提交成本（共通调用）
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer submitCost(Integer id, String costType) {

        CosSheet cosSheet = cosSheetRepository.getOne(id);
        // 取得本人项目下所有未提交数据
//        List<Integer> cosSheetIds = costMapper.findCosSheetIdByQuery(cosSheet);
//        String ids = StringUtils.join(cosSheetIds.toArray(), ",");
        // 更改成本主单（提交和驳回时使用）
        costMapper.updateCosSheetStatusSubmitOrRejectById(id, "", 2);
        logService.saveLog("提交" + costType + "成本信息", "主单ID：" + id+ "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 审核成本（共通调用）
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer checkCost(Integer id, String costType) {

        CosSheet cosSheet = cosSheetRepository.getOne(id);

        // 取得本人项目下所有未审核数据
//        List<Integer> cosSheetIds = costMapper.findCosSheetIdByQuery(cosSheet);
//        String ids = StringUtils.join(cosSheetIds.toArray(), ",");
        // 更改成本主单（审核）
        User user = ShiroUtils.getUser();
//        int verifyId =  user.getId();
        cosSheet.setId(id);
        cosSheet.setStatus(3);
        cosSheet.setVerifyCode(user.getAccount());
        cosSheet.setVerifyId(user.getId());
        cosSheet.setVerifyName(user.getName());
        cosSheet.setVerifyTime(new Date());
        costMapper.updateCosSheetStatusCheckById(cosSheet);
        logService.saveLog("审核" + costType + "成本信息", "主单ID：" + id + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 驳回成本（共通调用）
     *
     * @param id
     * @param rejectReason
     * @param costType
     * @return
     */
    public int rejectCost(Integer id, String rejectReason, String costType) {

        CosSheet cosSheet = cosSheetRepository.getOne(id);
        // 取得本人项目下所有未提交数据
//        List<Integer> cosSheetIds = costMapper.findCosSheetIdByQuery(cosSheet);
//        String ids = StringUtils.join(cosSheetIds.toArray(), ",");
        // 更改成本主单（提交和驳回时使用）
        costMapper.updateCosSheetStatusSubmitOrRejectById(id, rejectReason, 1);
        logService.saveLog("驳回" + costType + "成本信息", "主单ID：" + id + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 撤销成本（共通调用）
     */
//    @Transactional(rollbackFor = Exception.class)
//    public Integer cancelCost(Integer id, String costType) {
//
//        CosSheet cosSheet = cosSheetRepository.getOne(id);
//        // 取得本人项目下所有已审核数据
//        List<Integer> cosSheetIds = costMapper.findCosSheetIdByQuery(cosSheet);
//        String ids = StringUtils.join(cosSheetIds.toArray(), ",");
//
//        // 商务系统接收校验
//        if (!boolCheckCancel(ids)) {
//            return -1;
//        }
//
//        // 更改成本主单（撤销）
//        User user = ShiroUtils.getUser();
//        costMapper.updateCosSheetStatusCheckById(ids, 2,  user.getAccount(), user.getName(),new Date(),user.getId());
//        logService.saveLog("撤销" + costType + "成本信息", "主单ID：" + ids);
//        return 1;
//    }

    /**
     * 保存下料工时计划
     *
     * @param cosSheet
     * @param wQuantity
     * @param wUnit
     * @param wDescribe
     * @param wRemark
     * @param hQuantity
     * @param hUnit
     * @param hDescribe
     * @param hRemark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveBlanking(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -2;
        }
        cosSheet = saveCostSheet(cosSheet);
        saveCosDetail(wQuantity, wUnit, wDescribe, wRemark, "主要辅材", cosSheet.getId());
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "人工工时", cosSheet.getId());
        logService.saveLog(getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId()+ "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveheat(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -2;
        }
        cosSheet = saveCostSheet(cosSheet);
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "热处理", cosSheet.getId());
        logService.saveLog(getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }
    /**
     * 单个表单保存明细
     *
     * @param quantity
     * @param unit
     * @param describe
     * @param remark
     * @param dataType
     * @param cosSheetId
     * @return
     */
    public int saveCosDetail(Double[] quantity, String[] unit, String[] describe, String[] remark, String dataType, Integer cosSheetId) {
        if (describe == null) {
            return 1;
        }
        for (int i = 0; i < describe.length; i++) {
            CosDetail cosDetail = new CosDetail();
            cosDetail.setCosSheetId(cosSheetId);
            cosDetail.setDescribe(describe[i]);
            cosDetail.setQuantity(quantity[i]);
            cosDetail.setUnit(unit[i]);
            cosDetail.setDataType(dataType);
            if (remark.length > 0) {
                cosDetail.setRemark(remark[i]);
            }

            cosDetailRepository.save(cosDetail);
        }
        return 1;
    }

    /**
     * 运输的保存
     * @param model
     * @param id
     * @return
     */
    public int saveCosTra(CosCarriageDetail model,Integer id){
        model.setCosSheetId(id);
        cosCarriageDetailMapper.save(model);
        return 1;
    }
    /**
            * 运输的修改
     * @param model
     * @param id
     * @return
             */
    public int updCosTra(CosCarriageDetail model,Integer id){
        model.setCosSheetId(id);
        cosCarriageDetailMapper.updData(model);
        return 1;
    }


    /**
     * 修改原料保存
     *
     * @param cosSheet
     * @param wQuantity
     * @param wUnit
     * @param wDescribe
     * @param wRemark
     * @param hQuantity
     * @param hUnit
     * @param hDescribe
     * @param hRemark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveEditBlanking(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        // 重复校验
//        if (!boolCheckEdit(cosSheet)) {
//            return -2;
//        }
        // 保存主表--修改
        cosSheet = updCostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(wQuantity, wUnit, wDescribe, wRemark, "主要辅材", cosSheet.getId());
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "人工工时", cosSheet.getId());

        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }


    @Transactional(rollbackFor = Exception.class)
    public int saveUpdateBlanking(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        // 重复校验
//        if (!boolCheckEdit(cosSheet)) {
//            return -2;
//        }
        // 保存主表--修改
        cosSheet = upd2CostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(wQuantity, wUnit, wDescribe, wRemark, "主要辅材", cosSheet.getId());
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "人工工时", cosSheet.getId());

        logService.saveLog("变更"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 日志类别判定
     * @param cosSheet
     * @return
     */
    public String getCostType(CosSheet cosSheet){
        String value = "";
        switch (cosSheet.getCostType()){
            case 2:
                value="下料工时";
                break;
            case 3:
                value="铆焊工时";
                break;
            case 4:
                value="热处理";
                break;
            case 5:
                value="装配工时";
                break;
            case 6:
                value="涂装工时";
                break;
            case 7:
                value="安装工时";
                break;
            case 9:
                value="机加工时";
                break;
            case 10:
                value="包装运输";
                break;
        }
        return value;
    }
    /**
     * 热处理修改保存
     * @param cosSheet
     * @param wQuantity
     * @param wUnit
     * @param wDescribe
     * @param wRemark
     * @param hQuantity
     * @param hUnit
     * @param hDescribe
     * @param hRemark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveEditheat(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        // 重复校验
//        if (!boolCheckEdit(cosSheet)) {
//            return -2;
//        }
        // 保存主表
        cosSheet = updCostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "热处理", cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveUpdateheat(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        // 重复校验
//        if (!boolCheckEdit(cosSheet)) {
//            return -2;
//        }
        // 保存主表
        cosSheet = upd2CostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "热处理", cosSheet.getId());
        logService.saveLog("变更"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 喷涂保存
     * @param cosSheet
     * @param wpQuantity
     * @param wpUnit
     * @param wpDescribe
     * @param wpRemark
     * @param hpQuantity
     * @param hpUnit
     * @param hpDescribe
     * @param hpRemark
     * @param wtQuantity
     * @param wtUnit
     * @param wtDescribe
     * @param wtRemark
     * @param htQuantity
     * @param htUnit
     * @param htDescribe
     * @param htRemark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int savePaint(CosSheet cosSheet, Double[] wpQuantity, String[] wpUnit, String[] wpDescribe, String[] wpRemark, Double[] hpQuantity, String[] hpUnit, String[] hpDescribe, String[] hpRemark, Double[] wtQuantity, String[] wtUnit, String[] wtDescribe, String[] wtRemark, Double[] htQuantity, String[] htUnit, String[] htDescribe, String[] htRemark) {
        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -2;
        }
        cosSheet = saveCostSheet(cosSheet);
        saveCosDetail(wpQuantity, wpUnit, wpDescribe, wpRemark, "主要辅材喷", cosSheet.getId());
        saveCosDetail(hpQuantity, hpUnit, hpDescribe, hpRemark, "人工工时喷", cosSheet.getId());
        saveCosDetail(wtQuantity, wtUnit, wtDescribe, wtRemark, "主要辅材涂", cosSheet.getId());
        saveCosDetail(htQuantity, htUnit, htDescribe, htRemark, "人工工时涂", cosSheet.getId());
        logService.saveLog(getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }
    @Transactional(rollbackFor = Exception.class)
    public int saveEditPaint(CosSheet cosSheet, Double[] wpQuantity, String[] wpUnit, String[] wpDescribe, String[] wpRemark, Double[] hpQuantity, String[] hpUnit, String[] hpDescribe, String[] hpRemark, Double[] wtQuantity, String[] wtUnit, String[] wtDescribe, String[] wtRemark, Double[] htQuantity, String[] htUnit, String[] htDescribe, String[] htRemark) {
        // 重复校验
        if (!boolCheckEdit(cosSheet)) {
            return -2;
        }
        // 保存主表
        cosSheet = updCostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(wpQuantity, wpUnit, wpDescribe, wpRemark, "主要辅材喷", cosSheet.getId());
        saveCosDetail(hpQuantity, hpUnit, hpDescribe, hpRemark, "人工工时喷", cosSheet.getId());
        saveCosDetail(wtQuantity, wtUnit, wtDescribe, wtRemark, "主要辅材涂", cosSheet.getId());
        saveCosDetail(htQuantity, htUnit, htDescribe, htRemark, "人工工时涂", cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveUpdatePaint(CosSheet cosSheet, Double[] wpQuantity, String[] wpUnit, String[] wpDescribe, String[] wpRemark, Double[] hpQuantity, String[] hpUnit, String[] hpDescribe, String[] hpRemark, Double[] wtQuantity, String[] wtUnit, String[] wtDescribe, String[] wtRemark, Double[] htQuantity, String[] htUnit, String[] htDescribe, String[] htRemark) {
        // 重复校验
        if (!boolCheckEdit(cosSheet)) {
            return -2;
        }
        // 保存主表
        cosSheet = upd2CostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(wpQuantity, wpUnit, wpDescribe, wpRemark, "主要辅材喷", cosSheet.getId());
        saveCosDetail(hpQuantity, hpUnit, hpDescribe, hpRemark, "人工工时喷", cosSheet.getId());
        saveCosDetail(wtQuantity, wtUnit, wtDescribe, wtRemark, "主要辅材涂", cosSheet.getId());
        saveCosDetail(htQuantity, htUnit, htDescribe, htRemark, "人工工时涂", cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }

    /**
     * 成本信息输出查询
     * @param cosSheet
     * @return
     */
    public List<CosSheet> queryCossheet(CosSheet cosSheet) {
        return  costMapper.queryCossheet(cosSheet);
    }


    /**
     * 成本变更查询
     * @param cosSheet
     * @return
     */
    public List<CosSheet> queryCossheet2(CosSheet cosSheet) {
        cosSheet.setLoginName(ShiroUtils.getUser().getName());
        return  costMapper.queryCossheet2(cosSheet);
    }

    /**
     * 运输的保存
     * @param cosSheet
     * @param wQuantity
     * @param wUnit
     * @param wDescribe
     * @param wRemark
     * @param hQuantity
     * @param hUnit
     * @param hDescribe
     * @param hRemark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveTraBlanking(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark, CosCarriageDetail traModel) {
        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -2;
        }
        cosSheet = saveCostSheet(cosSheet);
        saveCosDetail(wQuantity, wUnit, wDescribe, wRemark, "主要辅材", cosSheet.getId());
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "人工工时", cosSheet.getId());
        saveCosTra(traModel,cosSheet.getId());
        logService.saveLog(getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());
        return 1;
    }
    /**
     * 修改原料保存
     *
     * @param cosSheet
     * @param wQuantity
     * @param wUnit
     * @param wDescribe
     * @param wRemark
     * @param hQuantity
     * @param hUnit
     * @param hDescribe
     * @param hRemark
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveEditTra(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark,CosCarriageDetail traModel) {
        // 重复校验
//        if (!boolCheckEdit(cosSheet)) {
//            return -2;
//        }
        // 保存主表--修改
        cosSheet = updCostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(wQuantity, wUnit, wDescribe, wRemark, "主要辅材", cosSheet.getId());
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "人工工时", cosSheet.getId());
        updCosTra(traModel,cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());

        return 1;
    }


    @Transactional(rollbackFor = Exception.class)
    public int saveUpdateTra(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark,CosCarriageDetail traModel) {
        // 重复校验
//        if (!boolCheckEdit(cosSheet)) {
//            return -2;
//        }
        // 保存主表--修改
        cosSheet = upd2CostSheet(cosSheet);
        // 删除明细
        delDsnCostDetail(cosSheet);
        // 保存明细
        saveCosDetail(wQuantity, wUnit, wDescribe, wRemark, "主要辅材", cosSheet.getId());
        saveCosDetail(hQuantity, hUnit, hDescribe, hRemark, "人工工时", cosSheet.getId());
        updCosTra(traModel,cosSheet.getId());
        logService.saveLog("变更"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId() + "   ,项目号："+cosSheet.getProCode()+"   ,项目名称："+cosSheet.getProName()+"   ,部分："+cosSheet.getProPart()+"   ,专业："+cosSheet.getSpecial());

        return 1;
    }






    /**
     * 保存机加工时录入
     * @param cosSheet
     * @param drawNo
     * @param drawName
     * @param drawNum
     * @param process
     * @param equName
     * @param quantity
     * @param remark
     * @return
     */
    public int saveMachining(CosSheet cosSheet, String[] drawNo, String[] drawName, Integer[] drawNum, String[] process, String[] equName, Double[] quantity, String[] remark) {

        // 重复校验
        if (!boolCheckSave(cosSheet)) {
            return -2;
        }
        // 保存主表
        cosSheet = saveCostSheet(cosSheet);
        // 保存明细
        saveCosMachiningDetail(drawNo, drawName, drawNum, process,equName,quantity,remark, cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId());
        return 1;
    }

    private int saveCosMachiningDetail(String[] drawNo, String[] drawName, Integer[] drawNum, String[] process, String[] equName, Double[] quantity, String[] remark, Integer cosSheetId) {
        if (drawNo == null) {
            return 1;
        }
        for (int i = 0; i < drawNo.length; i++) {
            CosMachiningDetail cosMachiningDetail = new CosMachiningDetail();
            cosMachiningDetail.setCosSheetId(cosSheetId);
            cosMachiningDetail.setDrawNo(drawNo[i]);
            cosMachiningDetail.setDrawName(drawName[i]);
            cosMachiningDetail.setDrawNum(drawNum[i]);
            cosMachiningDetail.setProcess(process[i]);
            cosMachiningDetail.setEquName(equName[i]);
            cosMachiningDetail.setQuantity(quantity[i]);
            cosMachiningDetail.setDataType("机加工时");
            if (remark.length > 0) {
                cosMachiningDetail.setRemark(remark[i]);
            }
            cosMachiningDetail = cosMachiningDetailRepository.save(cosMachiningDetail);
        }
        return  1;
    }

    public int saveEditMachining(CosSheet cosSheet, CosSheet cosSheet1, String[] drawNo, String[] drawName, Integer[] drawNum, String[] process, String[] equName, Double[] quantity, String[] remark) {

        // 重复校验
        if (!boolCheckEdit(cosSheet)) {
            return -2;
        }
        // 保存主表
        cosSheet = updCostSheet(cosSheet);
        // 删除明细
        delMachiningDetail(cosSheet);
        // 保存明细
        saveCosMachiningDetail(drawNo, drawName, drawNum, process,equName,quantity,remark, cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"确认单", "确认单ID：" + cosSheet.getId());
        return 1;
    }

    public int saveUpdateMachining(CosSheet cosSheet, CosSheet cosSheet1, String[] drawNo, String[] drawName, Integer[] drawNum, String[] process, String[] equName, Double[] quantity, String[] remark) {

        // 重复校验
        if (!boolCheckEdit(cosSheet)) {
            return -2;
        }
        // 保存主表
        cosSheet = upd2CostSheet(cosSheet);
        // 删除明细
        delMachiningDetail(cosSheet);
        // 保存明细
        saveCosMachiningDetail(drawNo, drawName, drawNum, process,equName,quantity,remark, cosSheet.getId());
        logService.saveLog("修改"+getCostType(cosSheet)+"计划单", "确认单ID：" + cosSheet.getId());
        return 1;
    }

    /**
     * 按 主单ID 查询机加成本明细
     * @return
     */
    public  List<CosMachiningDetail> findMachiningCosDetailByCosSheetIdAndDataType(Integer cosSheetId, String dataType){
        return costMapper.findMachiningCosDetailByCosSheetIdAndDataTypeeetIdAndDataType(cosSheetId, dataType);
    }


    /**
     * 获取机加工序
     * @return
     */
    public List<CosProcess> findProcesses() {
        return  dsnCostMapper.findProcesses();
    }

    public List<CosProcessDetail> findEqu() {
        return  dsnCostMapper.findEqu();
    }

    /**
     * 根据工序名称查询设备
     * @param name
     * @return
     */
    public List<CosProcessDetail> findBufByName(String name) {
        return  dsnCostMapper.findBufByName(name);
    }
}
